package com.interview.javabasic.IO;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2021/2/28

*/
public class IOServer {
    public static void main(String[] args) throws IOException {
        //服务端处理客户端的连接请求
        ServerSocket serverSocket = new ServerSocket(3333);

        //接收到客户端连接请求之后为每个客户端创建一个新的线程进行链路处理
        new Thread(() ->{
            while (true){
                try {
                    Socket accept = serverSocket.accept();
                    int len;
                    byte[] data = new byte[1024];
                    InputStream inputStream = accept.getInputStream();
                    //按字节流的方式读取数据
                    while ((len = inputStream.read(data))!= -1){
                        System.out.println(new String(data,0,len));
                    }
                } catch (IOException e) {
                }
            }
        }).start();
    }
}
